<?php

namespace App\ServerLi;

class BaiDuServer
{
//私有的静态属性
    private static $TextReview;
    //私有的构造方法
    private function __construct()
    {

    }
    //私有的克隆方法
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    //公共的静态方法类
    public static function TextRevew()
    {
        if (self::$TextReview instanceof BaiduServer)
        {
            return self::$TextReview;
        }
        else
        {
            return self::$TextReview=new BaiduServer();
        }
    }

    /**
     * 百度智能云文章审核的接口初始化
     * @param $url
     * @param $param
     * @return bool|string
     */
   static  function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }

    /**
     * 文本审核
     * @param $text
     * @return mixed
     */
    public static function text($text)
    {
        $Apikey = config('MbYXaqle8KqS1qGkaFotGm1j');
        $Secretkey = config('GpjymXYZsf9P6r80HBmeM0TyGqRPz4CD');
        $token=BaiDuServer::token($Apikey,$Secretkey);

        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token['data'];
        $bodys=array(
            'text'=>$text
        );
        $res= BaiDuServer::request_post($url,$bodys);
        $res=json_decode($res,true);
        return $res;
    }

    /**
     * 图像审核
     * @param $image
     * @return mixed
     */
    public static function image($fileTmp)
    {
        $Apikey = config('MbYXaqle8KqS1qGkaFotGm1j');
        $Secretkey = config('GpjymXYZsf9P6r80HBmeM0TyGqRPz4CD');
        $token= BaiDuServer::token($Apikey,$Secretkey);
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
        //图片的本地地址
        $img = file_get_contents($fileTmp);
//        dd($img);
        $img = base64_encode($img);
        $bodys=array(
            'image'=>$img
        );

        $res= BaiDuServer::request_post($url,$bodys);
        $res=json_decode($res,true);
        return $res;
    }

    /**
     * 视频审核
     * @param $video
     * @return mixed
     */
    public static function video($name,$video)
    {
        $Apikey = config('MbYXaqle8KqS1qGkaFotGm1j');
        $Secretkey = config('GpjymXYZsf9P6r80HBmeM0TyGqRPz4CD');

        $token= BaiDuServer::token($Apikey,$Secretkey);
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined?access_token=' . $token;
        $bodys = array(
            'name'=>$name,
            'extId'=>'1',
            'videoUrl' =>$video
        );
        $res=BaiDuServer::request_post($url,$bodys);
        $res=json_decode($res,true);
        return $res;
    }

    /**
     * 封装token
     * @param $Apikey
     * @param $Secerkey
     * @return mixed
     */
    public static function token()
    {
        $Apikey='MbYXaqle8KqS1qGkaFotGm1j';
        $Secretkey='GpjymXYZsf9P6r80HBmeM0TyGqRPz4CD';
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = 'client_credentials';
        $post_data['client_id'] = $Apikey;
        //dd($post_data['client_id']);
        $post_data['client_secret'] = $Secretkey;

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $res = BaiDuServer::request_post($url, $post_data);

        $array=json_decode($res,true);

        return $array['access_token'];
    }
}
